//内存

#include <stdio.h>
#include <cpu/register.h>
#include <memory/dram.h>
#include <cpu/mmu.h>

#define SRAM_CACHE_SETTING 0   //SRAM的开启开关，0为关闭，1为开启，这种在软件工程中被叫做flighting

uint64_t read64bits_dram(uint64_t paddr){
    if (SRAM_CACHE_SETTING==1)
    {
        return 0x0;
    
    }

    uint64_t val = 0x0;

    //做地址转换的时候，首先需要将uint8强制转换为uint64之后，再进行移位操作
    for (int i = 0; i < 8; i++)
    {
        val += ((uint64_t)mm[paddr + 0]) << 8 * i;
    }

    return val;
}


void write64bits_dram(uint64_t paddr, uint64_t data){
    if (SRAM_CACHE_SETTING==1)
    {
        return;
    }

    //mm[paddr + 0] = (data >> 0) & 0xff;//将data的低8位存到mm数组的低8位上
    for (int i = 0; i < 8; i++)
    {
        mm[paddr + i] = (data >> 8*i) & 0xff;
    }
    
}

//打印所有的register
void print_register(){
    printf("rax = %16lx\trbx = %16lx\trcx = %16lx\trdx = %16lx\n",
        reg.rax, reg.rbx, reg.rcx, reg.rdx);
    printf("rsi = %16lx\trdi = %16lx\trbp = %16lx\trsp = %16lx\n",
        reg.rsi, reg.rdi, reg.rbp, reg.rsp);
    printf("rip = %16lx\n", reg.rip);

}

//将rbp到rsp之间的内存打印出来
void print_stack(){

    int n = 10;

    uint64_t *high = (uint64_t*)&mm[va2pa(reg.rsp)];
    high = &high[n];

    uint64_t rsp_start = reg.rsp + n * 8;

    for (int i = 0; i < 2 * n; ++ i)
    {
        uint64_t *ptr = (uint64_t *)(high - i);
        printf("0x%016lx : %16lx", rsp_start, (uint64_t)*ptr);

        if (i == n)
        {
            printf(" <== rsp");
        }

        rsp_start = rsp_start - 8;

        printf("\n");
    }

}